<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :rules="rules"
      :model="loginForm"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">IHRM 后台登录系统</h3>
      </div>
      <!-- 手机号区域 -->
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.mobile" placeholder="请输入手机号" type="text" auto-complete="on" />
      </el-form-item>
      <!-- 密码区域 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.password"
          :type="passwordType ? 'text' : 'password'"
          placeholder="请收入密码"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="changePasswordType">
          <svg-icon :icon-class="passwordType ? 'eye-open' : 'eye'" />
        </span>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button
        type="primary"
        :loading="loading"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>
      <!-- 提示信息 -->
      <div class="tips">
        <span style="margin-right: 20px">username: 13800000000</span>
        <span> password: 111111</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  name: 'Login',

  data() {
    // 自定义校验手机号
		// .test()方法，用于检测某个字符串是否匹配某个模式，匹配返回true，否则返回false
    var mobileRules = (rule, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        callback();
      } else {
        return callback(new Error('请输入正确的手机号'));
      }
    };

    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      passwordType: false,
      rules: {
        mobile: [
          {
            required: true,
            trigger: 'blur',
						message: '手机号不能为空',
            validator: mobileRules
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '密码不能为空'
          },
					{
						min: 6,
						max: 16,
						trigger: blur,
						message: '密码应该在 6-16 位之间'
					}
        ]
      },
      loading: false
    };
  },
  computed: {
    ...mapState(['token'])
  },

  methods: {
    ...mapActions('loginModule', ['asyncLoginToken']),
    // ——————————————————————————————————————————————————————————————————————————————————————————————————
    // 密码框中的小眼睛的状态
    changePasswordType() {
      this.passwordType = !this.passwordType;
    },

    // ——————————————————————————————————————————————————————————————————————————————————————————————————

    // 点击登录按钮
    // validate:  element-ui中，进行表单校验的方法
   async handleLogin() {
      this.loading = true
      this.$refs.loginForm.validate(async isOk => { // 1、先进行表单验证
        if (isOk) {
          try {
          await this.asyncLoginToken(this.loginForm);// 2、调用vuex中的登录接口，进行登录
          this.$router.push('/')// 3、跳转到首页
          } catch (err) {
            console.log(err);
          } finally { // finally是，不管是否成功（ try/catch ）都会执行的代码体
            this.loading = false
          }
        }
      });
    }
  }
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$login_bg: #407ffe;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(../../assets/common/login.jpg) no-repeat scroll top/cover;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  ::v-deep .el-button--primary {
    background-color: $login_bg;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 41px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: normal;
    }
  }
  .el-form-item {
    ::v-deep .el-form-item__content {
      background-color: #fff;
      border-radius: 5px;
      opacity: 0.8;
      .el-input {
        input {
          color: #283443;
        }
      }
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
